package com.sunlands.zlcx.usercenter.processor.online.chain;

import com.sunlands.zlcx.usercenter.processor.online.OnlineProcessor;
import com.sunlands.zlcx.usercenter.util.SpringContextHolder;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: anjl
 * @description: 用户上线后的处理逻辑
 * @data: 2020-09-27
 **/
@Component
public class OnlineProcessorChain {

    private volatile List<OnlineProcessor> chain = null;
    public void init(Long userId) {
        initChainIfNull();
        chain.forEach(processor -> processor.init(userId));
    }

    private void initChainIfNull() {
        if(Objects.isNull(chain)){
            synchronized (OnlineProcessorChain.class){
                if(Objects.isNull(chain)){
                    chain = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(OnlineProcessor.class).values());
                    AnnotationAwareOrderComparator.sort(chain);
                }
            }
        }
    }
}
